#ifndef _RULECOMPILER_H_
#define _RULECOMPILER_H_

#include <unordered_map>

#include "Operation.h"
#include "Field.h"

class AbstractRule;
class ComplexRule;
class ProxyRule;
class EqualsRule;
class DifferRule;

/*
 * RuleCompiler
 */

class RuleField {
	unsigned int word;
	unsigned int bit;
	unsigned int length;
	bool isTcp;

	friend class RuleCompiler;
public:
	RuleField() {}
	RuleField(unsigned int word, unsigned int bit,
						unsigned int length, bool tcp) :
		word(word), bit(bit), length(length), isTcp(tcp) {}
};

class RuleCompiler {
	std::unordered_map<std::string, AbstractRule *> templates;
	std::unordered_map<std::string, RuleField> fields;

	~RuleCompiler();
	RuleCompiler();
	RuleCompiler(const RuleCompiler &);
	RuleCompiler &operator=(const RuleCompiler &);

	const AbstractRule *getRule(const Operation &);
public:
	void compile(AbstractRule &rule, const Operation &operation);

	void compile(ComplexRule &rule, const Operation &o);
	void compile(ProxyRule &rule, const Operation &o);
	void compile(EqualsRule &rule, const Operation &o);
	void compile(DifferRule &rule, const Operation &o);

	static RuleCompiler &getInstance(void);
	std::pair<size_t, size_t> parseFields(const std::vector<Field> &);
};

#endif
